import axios from 'axios'

export const getCurrentCity = () => {
  const localCity = JSON.parse(localStorage.getItem('hkzf_city'))
  // localStorage没有数据
  if (!localCity) {
    return new Promise((resolve, reject) => {
      // 百度地图获取当前城市
      const curCity = new window.BMapGL.LocalCity()
      curCity.get(async res => {
        try {
          const result = await axios.get(
            `http://localhost:8080/area/info?name=${res.name}`
          )
          // 存储到本地存储中
          localStorage.setItem('hkzf_city', JSON.stringify(result.data.body))
          // 返回该城市数据
          resolve(result.data.body)
        } catch (e) {
          // 获取定位城市失败
          reject(e)
        }
      })
    })
  }
  return Promise.resolve(localCity)
}